<?php

namespace app\model;
use app\model\CourierInfo as ModelCourierInfo;
use app\model\DriverInfo as ModelDriverInfo;
use app\model\StationInfo as ModelStationInfo;

use think\Db;

class WithdrawLog extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'withdraw_log';
    }

    /**
     * 类入口
     * @return WithdrawLog
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /*
     * 添加提现记录
     * $type:1:快递员；2：司机；3：站点；
     */
    public function addWithdrawLog($mapId,$bankCardId,$money,$type,$bankCardNumber,$bankName,$realName,$serverCharge)
    {

        switch ($type){
            case 1 :
                $obj = ModelCourierInfo::getInstance();
                break;
            case 2 :
                $obj = ModelDriverInfo::getInstance();
                break;
            case 3 :
                $obj = ModelStationInfo::getInstance();
                break;
            default:
                _returnError(10014);
                break;
        }
        $balance = $obj->toValue(['id'=>$mapId],'balance') - $money - $serverCharge;
        $add['map_id'] = $mapId;
        $add['bank_card_id'] = $bankCardId;
        $add['money'] = $money;
        $add['balance'] = $balance ;
        $add['type'] = $type;
        $add['bank_card_number'] = $bankCardNumber;
        $add['state'] = 1;
        $add['withdraw_number'] = $this->createWithdrawNumber();
        $add['bank_name'] = $bankName;
        $add['real_name'] = $realName;
        $add['server_charge'] = $serverCharge;
        $add['base_balance'] = $obj->toValue(['id'=>$mapId],'balance');

        return $this->toAdd($add);
    }

    /*
     * 生成提现单号
     */
    public function createWithdrawNumber()
    {
        return 'TX' . date('YmdHis', time()) . mt_rand(10000, 99999);
    }

}
